package com.bdqn.wang.dao.impl;

import com.bdqn.wang.dao.ComdityAdmDao;
import com.bdqn.wang.pojo.ComdityAdm;
import com.bdqn.wang.pojo.Menber;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ComdityAdmDaoImpl extends BaseDao implements ComdityAdmDao {
    @Override
    public List<ComdityAdm> getAll() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            String sql = "select * from easybuy_product where isDelete = 0";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            List<ComdityAdm> comdityAdmList = new ArrayList<ComdityAdm>();
            while (resultSet.next()){
                ComdityAdm comdityAdm = new ComdityAdm();
                comdityAdm.setId(resultSet.getInt(1));
                comdityAdm.setName(resultSet.getString(2));
                comdityAdm.setDescription(resultSet.getString(3));
                comdityAdm.setPrice(resultSet.getInt(4));
                comdityAdm.setStock(resultSet.getInt(5));
                comdityAdm.setCategoryLevel1Id(resultSet.getInt(6));
                comdityAdm.setCategoryLevel2Id(resultSet.getInt(7));
                comdityAdm.setCategoryLevel3Id(resultSet.getInt(8));
                comdityAdm.setFileName(resultSet.getString(9));
                comdityAdm.getIsDelete(resultSet.getInt(10));
                comdityAdmList.add(comdityAdm);
            }
            return comdityAdmList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return null;
    }


    @Override
    public int del(int id) {
        String sql="update easybuy_product set isDelete = 1 where id = ?";
        Object [] obj = {id};
        return executeUpdate(sql,obj);
    }

    @Override
    public int add(ComdityAdm comdityAdm) {
        String sql = "insert into easybuy_product values(default,?,?,?,?,?,?,?,?,default)";
        Object[] obj = {

                comdityAdm.getName(),
                comdityAdm.getDescription(),
                comdityAdm.getPrice(),
                comdityAdm.getStock(),
                comdityAdm.getCategoryLevel1Id(),
                comdityAdm.getCategoryLevel2Id(),
                comdityAdm.getCategoryLevel3Id(),
                comdityAdm.getFileName(),


        };
        return executeUpdate(sql,obj);
    }

    @Override
    public int update(ComdityAdm comdityAdm) {
        String sql = "update easybuy_product set name=?, " +
                "description=?, price=?, stock=?," +
                " categoryLevel1Id=?, categoryLevel2Id=?, categoryLevel3Id=?," +
                " fileName=? " +
                "where id=?";
        Object[] obj = {
                comdityAdm.getName(),
                comdityAdm.getDescription(),
                comdityAdm.getPrice(),
                comdityAdm.getStock(),
                comdityAdm.getCategoryLevel1Id(),
                comdityAdm.getCategoryLevel2Id(),
                comdityAdm.getCategoryLevel3Id(),
                comdityAdm.getFileName(),
                comdityAdm.getId()
        };
        return executeUpdate(sql,obj);
    }

    @Override
    public ComdityAdm getcomdityAdmById(int id) {
        Connection connection =null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection=getConnection();
            String sql="select id,easybuy_product.name ,easybuy_product.price,easybuy_product.stock,easybuy_product.categoryLevel1Id,easybuy_product.categoryLevel2Id,easybuy_product.categoryLevel3Id,easybuy_product.fileName  from easybuy_product where id= ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,id);
            resultSet = preparedStatement.executeQuery();
            ComdityAdm comdityAdm = new ComdityAdm();
            if (resultSet.next()){
                comdityAdm.setId(resultSet.getInt(1));
                comdityAdm.setName(resultSet.getString(2));
                comdityAdm.setPrice(resultSet.getInt(3));
                comdityAdm.setStock(resultSet.getInt(4));
                comdityAdm.setCategoryLevel1Id(resultSet.getInt(5));
                comdityAdm.setCategoryLevel2Id(resultSet.getInt(6));
                comdityAdm.setCategoryLevel3Id(resultSet.getInt(7));
                comdityAdm.setFileName(resultSet.getString(8));
            }
            return comdityAdm;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return null;
    }




}
